package com.wq.jdk8;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * @Author: wangqiang20995
 * @Date:2019/7/11
 * @Description:
 * @Resource:
 */
public class ThreadInterrupt {


    public static void main(String[] args) throws InterruptedException {
        Runnable worker = new WorkerThread();

        Thread thread = new Thread(worker);
        ((WorkerThread) worker).setSelf(thread);
        thread.start();

        TimeUnit.SECONDS.sleep(1);

        //不会中断挂起的线程，只是去唤醒
//        LockSupport.unpark(thread);

        //不去唤醒，直接去中断
        thread.interrupt();
    }
}

class WorkerThread implements Runnable{

    private Thread self;

    public void setSelf(Thread self) {
        this.self = self;
    }

    @Override
    public void run() {
        LockSupport.park(this);

        //当前的线程是否被中断，如果是，清除中断状态
        boolean interrupt = Thread.interrupted();
        System.out.println("Thread.interrupted():" + interrupt);
        System.out.println("is thread interrupted:" + Thread.currentThread().isInterrupted());
        System.out.println("is thread interrupted:" + self.isInterrupted());

        Thread.currentThread().interrupt();
        System.out.println("Thread.interrupted():" + Thread.interrupted());
        System.out.println("Thread.interrupted():" + Thread.interrupted());
    }
}